package com.shiro.demo.Session;

import com.shiro.demo.util.JedisUtil;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.SerializationUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class redisSessionDao extends AbstractSessionDAO {
    @Autowired
    JedisUtil jedisUtil;

    private final String SHIRO_SESSION_PREFIX ="xcl-session:";
    private byte[] getKey(String Key){
        return (SHIRO_SESSION_PREFIX+Key).getBytes();
   }
   private void  saveSession(Session session){
        if (session!=null&&session.getId()!=null){
       byte[] key=getKey(session.getId().toString());
       byte[] value=SerializationUtils.serialize(session);
       jedisUtil.set(key,value);
       jedisUtil.expire(key,600);
        }

   }
    @Override
    protected Serializable doCreate(Session session) {
        Serializable serializable=generateSessionId(session);
        assignSessionId(session,serializable);
             saveSession(session);
        return serializable;
    }

    @Override
    protected Session doReadSession(Serializable sessionId) {
        System.out.println("read session");
        if (sessionId==null){
            return  null;
        }
        byte[] key=getKey(sessionId.toString());
        byte[] value=jedisUtil.get(key);
        return (Session) SerializationUtils.deserialize(value);
    }

    @Override
    public void update(Session session) throws UnknownSessionException {
       saveSession(session);
    }

    @Override
    public void delete(Session session) {
        if (session==null&&session.getId()==null){
            return;
        }
        byte[] key=getKey(session.getId().toString());
        jedisUtil.del(key);
    }

    @Override
    public Collection<Session> getActiveSessions() {
        Set<byte[]>  keys=jedisUtil.keys(SHIRO_SESSION_PREFIX);
        Set<Session>sessions=new HashSet<>();
        if(CollectionUtils.isEmpty(keys)){
            return sessions;
        }
        for(byte[] key:keys){
            Session session= (Session) SerializationUtils.deserialize(jedisUtil.get(key));
            sessions.add(session);
        }
        return sessions;
    }
}
